#ifndef USTRING_H
#define USTRING_H

#if 0
#include <QString>
#include <string>

class UString : public QString {

public:
    using QString::QString;

    UString(const QString& str):
        QString(str){}
    UString(const std::string& str):
        QString(QString::fromStdString(str)){}
    UString(const char* str):
        QString(str){}

    // 隐式转换为const char*
    operator const char*() const {
        static std::string str = this->toStdString();
        return str.c_str();
    }
    // 隐式转换为std::string
    operator std::string() const {
        return this->toStdString();
    }

    // 赋值运算符重载，万能字符串能直接赋值给const char*,std::string
    UString& operator=(const char* str) {
        this->QString::operator=(str);
        return *this;
    }

    UString& operator=(const std::string& str) {
        this->QString::operator=(QString::fromStdString(str));
        return *this;
    }

    UString& operator=(const QString str) {
        this->QString::operator=(str);
        return *this;
    }
    // 字符串拼接
    UString& operator+=(const UString& other) {
        this->QString::operator+=(other);
        return *this;
    }

    UString& operator+=(const char* str) {
        this->QString::operator+=(str);
        return *this;
    }

    UString& operator+=(const std::string& str) {
        this->QString::operator+=(QString::fromStdString(str));
        return *this;
    }

    // append方法
    UString& append(const UString& other) {
        this->QString::append(other);
        return *this;
    }

    UString& append(const char* str) {
        this->QString::append(str);
        return *this;
    }

    UString& append(const std::string& str) {
        this->QString::append(QString::fromStdString(str));
        return *this;
    }

    // 重载+运算符
    UString operator+(const UString& other) {
        UString result = *this;
        result.append(other);
        return result;
    }

    UString operator+(const char* str) {
        UString result = *this;
        result.append(str);
        return result;
    }

    UString operator+(const std::string& str) {
        UString result = *this;
        result.append(QString::fromStdString(str));
        return result;
    }
};


#else
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>

#ifdef QT_CORE_LIB
#include <QString>
#include <QDebug>
#endif

class UString : public std::u32string
{
public:
    // 构造函数
    UString() = default;

    UString(const std::u32string& str)
        : std::u32string(str)
    {

    }

    UString(const std::string& str)
    {
        std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
        std::u32string utf32_str = converter.from_bytes(str);
        assign(utf32_str);
    }

    UString(const char* str)
    {
        assign(from_bytes(str));
    }

#ifdef QT_CORE_LIB
    UString(const QString& str):
        std::u32string(str.toStdU32String())
    {

    }
#endif

    // 赋值操作符
    UString& operator=(const std::u32string& str)
    {
        std::u32string::operator=(str);
        return *this;
    }

    UString& operator=(const std::string& str)
    {
        std::u32string::operator=(from_bytes(str));
        return *this;
    }

    UString& operator=(const char* str)
    {
        std::u32string::operator=(from_bytes(str));
        return *this;
    }

#ifdef QT_CORE_LIB
    UString& operator=(const QString& str)
    {
        std::u32string::operator=(str.toStdU32String());
        return *this;
    }
#endif

    // 类型转换函数
    std::string to_u8str() const
    {
        return to_bytes();
    }

    const char* to_cstr() const
    {
        static std::string utf8_str = to_bytes();
        return utf8_str.c_str();
    }
#ifdef QT_CORE_LIB
    QString to_qstr() const
    {
        return QString::fromStdU32String(*this);
    }
#endif

    // 隐式转换
    operator std::string() const
    {
        return this->to_u8str();
    }

    operator const char*() const
    {
        return this->to_cstr();
    }

#ifdef QT_CORE_LIB
    operator QString() const
    {
        return this->to_qstr();
    }
#endif

    // 输出重定向
    friend std::ostream& operator<<(std::ostream& os, const UString& obj)
    {
        std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
        os << converter.to_bytes(obj);
        return os;
    }

#ifdef QT_CORE_LIB
    friend QDebug& operator<<(QDebug& debug, const UString& obj)
    {
        debug << QString::fromStdU32String(obj);
        return debug;
    }
#endif

    // 字符串拼接
    UString& operator+=(const UString& str) {
        std::u32string::operator+=(str);
        return *this;
    }

    UString& operator+=(const char* str)
    {
        std::u32string::operator+=(from_bytes(str));
        return *this;
    }

    UString& operator+=(const std::string& str)
    {
        std::u32string::operator+=(from_bytes(str));
        return *this;
    }
#ifdef QT_CORE_LIB
    UString& operator+=(const QString& str)
    {
        std::u32string::operator+=(str.toStdU32String());
        return *this;
    }
#endif

    // append方法
    UString& append(const UString& str)
    {
        std::u32string::append(str);
        return *this;
    }

    UString& append(const char* str)
    {
        std::u32string::append(from_bytes(str));
        return *this;
    }

    UString& append(const std::string& str)
    {
        std::u32string::append(from_bytes(str));
        return *this;
    }
#ifdef QT_CORE_LIB
    UString& append(const QString& str)
    {
        std::u32string::append(str.toStdU32String());
        return *this;
    }
#endif

    // 重载+运算符
    UString operator+(const UString& str)
    {
        UString result = *this;
        result.append(str);
        return result;
    }

    UString operator+(const char* str)
    {
        UString result = *this;
        result.append(str);
        return result;
    }

    UString operator+(const std::string& str)
    {
        UString result = *this;
        result.append(str);
        return result;
    }
#ifdef QT_CORE_LIB
    UString operator+(const QString& str)
    {
        UString result = *this;
        result.append(str.toStdU32String());
        return result;
    }
#endif

    // 重载==运算符
    bool operator==(const UString& str) const
    {
        auto str1 = static_cast<const std::u32string&>(*this);
        auto str2 = static_cast<const std::u32string&>(str);
        return (str1 == str2);
    }

    bool operator==(const char* str) const
    {
        return static_cast<const std::u32string&>(*this) == from_bytes(str);
    }

#ifdef QT_CORE_LIB
    bool operator==(const QString& str) const
    {
        return static_cast<const std::u32string&>(*this) == str.toStdU32String();
    }
#endif

private:
    std::u32string from_bytes(const char* str) const
    {
        std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
        return converter.from_bytes(str);;
    }

    std::u32string from_bytes(const std::string& str) const
    {
        std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
        return converter.from_bytes(str);
    }

    std::string to_bytes() const
    {
        std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
        return converter.to_bytes(*this);
    }
};
#endif

#endif // USTRING_H

